package com.jrx.anytxn.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.customer.dto.coupon.TlCouponDetailRes;
import com.jrx.anytxn.customer.entity.CmCustomerRightInfo;
import com.jrx.anytxn.customer.entity.TlCouponDetail;
import com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerRightInfoMapper;
import com.jrx.anytxn.customer.service.ICouponDetailService;
import com.jrx.anytxn.customer.service.ITlCouponDetailService;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.param.entity.TlCouponSummary;
import com.jrx.anytxn.param.service.couponReport.ITlCouponSummaryService;

@Service
public class CouponDetailServiceImpl implements ICouponDetailService {

    @Autowired
    private ITlCouponDetailService tlCouponDetailService;
    @Resource
    private ExtCmCustomerRightInfoMapper extCmCustomerRightInfoMapper;
    @Autowired
    private ITlCouponSummaryService tlCouponSummaryService;

    @Override
    public TxnPage<TlCouponDetailRes> findAllPageByTenantId(Integer pageNum, Integer pageSize, String tenantId, String chineseName, String customerId, String idNumber, String updateBy, String mobile, String beginDate, String endDate) throws TxnException {
        List<TlCouponDetail> tlCouponDetailList = tlCouponDetailService.findAllPageByTenantId(pageNum, pageSize,tenantId,chineseName,  customerId,  idNumber,  updateBy,  mobile, beginDate, endDate);

        int selectCount = tlCouponDetailService.countAllPageByTenantId(pageNum, pageSize,tenantId,chineseName,  customerId,  idNumber,  updateBy,  mobile, beginDate, endDate);

        List<TlCouponDetailRes> resList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(tlCouponDetailList)){
            for(TlCouponDetail detail : tlCouponDetailList){
                TlCouponDetailRes res = BeanMapping.copy(detail, TlCouponDetailRes.class);
                CmCustomerRightInfo cmCustomerRightInfo = extCmCustomerRightInfoMapper.selectByCustRightId(detail.getCustomerRightsId(),detail.getCustomerId(),detail.getTenantId());
                res.setRightsStatus(cmCustomerRightInfo.getRightsStatus());

                //根据id查询汇总表信息
                TlCouponSummary summary = tlCouponSummaryService.findById(detail.getCouponSummaryId(),detail.getTenantId());
                res.setFileName(summary.getFileName());
                resList.add(res);
            }
        }
        return new TxnPage<>(pageNum, pageSize, selectCount, resList);
    }
}
